<?php
namespace app\authorization\controller;

use cmf\controller\AdminBaseController;
use app\authorization\model\ModuleModel as MM;

/**
 * 20200415
 * 模块授权管理
 */
class RbacModuleController extends AdminBaseController
{
	private $moduleMode;
	public function _initialize(){
		$this->moduleMode = new MM();
	}
	/**
	 * 模块授权首页
	 */
	public function index()
	{
		$param = $this->request->param();
		$company = isset( $param ['company']  ) ? $param [ 'company' ] : '';
		$companyList = $this->moduleMode->getCompanyList();
		$list = [];
		if( $company != '' ){
			$list  = $this->moduleMode->getModuleListByCompanyId( $company );
		}
		$this->assign( 'company' , $company );
		$this->assign( 'companyList' , $companyList );
		$this->assign( 'list' , $list );
		return $this->fetch('rbac_menu/module/index');
	}	

	/**
	 * 选择模块
	 */
	public function selectModule()
	{
		$param = $this->request->param();
		$cid = $param [ 'cid' ];
		if( !$this->request->isPost() ){
			$moduleList = $this->moduleMode->selectModule( $cid );
			$this->assign( 'list' , json_encode( $moduleList , JSON_UNESCAPED_UNICODE ) );
			return $this->fetch( 'rbac_menu/module/select_module' );
		}else{
			$res = $this->moduleMode->saveAuthModule( $cid , $param['list'] );
			if( !$res ){
				return api_json_echo( false , '保存授权数据失败,请稍后再试!' , null , 100 );
			}
			return api_json_echo( true , '授权信息保存成功!' );
		}
		
	}

	/**
	 * 取消授权
	 */
	public function delete()
	{
		$param = $this->request->param();
		$cid = $param [ 'cid' ];
		$storeId = $param [ 'id' ];
		$res = $this->moduleMode->deleteModuleMenu( $cid , $storeId );
		if( !$res ){
			return api_json_echo( false , '操作失败,请稍后再试' , null , 100 );
		}
		return api_json_echo( true , '操作成功');
	}
}